var app = getApp();
var util = require('../../../../utils/util.js');

Page({
    data: {
        imgUrl: app.globalData.imgUrl,
        pageCurrent: 1,
        pageNum: "",  //总页数
        orderList:[],
        orderArr:"",
        orderLength:"",
        orderStatus:"",
        dateSign:"",
        titleStatus:'0',
        typeList:[[{name:'住房订单',id:'1'},{name:'服务订单',id:'-1'}]],
        typeId:1,
        typeVal: "住房订单",
        typeFlag: false,
        typePickerShow: false,
        dateList:[[{name:'全部',sign:''},{name:'最近1周',sign:'week'},{name:'最近1个月',sign:'month'},{name:'最近6个月',sign:'halfYear'},{name:'最近1年',sign:'year'},{name:'1年以上',sign:'agoYear'}]],
        dateVal: "全部",
        dateFlag: false,
        datePickerShow: false,
        loading:false,   //"上拉加载中"
        loadingComplete:false,   //"加载完成"
    },
    onLoad: function () {
        var that = this;
        that.setData({
            navH: app.globalData.navHeight,
        })
        that.getOrder('0','1','','');
    },
    navBack: function () {
        wx.switchTab({
            url: '../../index/my-index'
        })
    },
    getOrder: function (status,order_type,starttime,endtime) {
        var that = this;
        var dataParams={
            token: wx.getStorageSync('token'),
            page: that.data.pageCurrent,
            pagesize: 6
        };
        if(status){
            dataParams['status']=status;
        }
        if(order_type){
            dataParams['order_type']=order_type;
        }
        if(starttime){
            dataParams['starttime']=starttime;
        }
        if(endtime){
            dataParams['endtime']=endtime;
        }
        wx.request({
            url: app.globalData.apiHost + "/api/order/orderList",
            data: dataParams,
            header: {
                'content-type': 'application/x-www-form-urlencoded',
                'Cookie': 'token=' + wx.getStorageSync('token')
            },
            method: "POST",
            dataType: "json",
            success: function (res) {
                if (res.statusCode === 200) {
                    var listLength=res.data.data.data.length;
                    var listPageNum = res.data.data.last_page;
                    for (var i = 0; i < res.data.data.data.length; i++) { 
                        that.data.orderList.push(res.data.data.data[i]);
                    }
                    var orderData = that.data.orderList;
                    that.setData({
                        orderArr: orderData,
                        pageNum:listPageNum,
                        orderLength:listLength
                    })
                }
            },
            
        });
    },
    titleCheck:function(e){
      var that=this;
      that.setData({
          titleStatus:e.currentTarget.dataset.index,
          orderStatus:e.currentTarget.dataset.status,
          orderList:[],
          pageCurrent:1
      });
      that.orderSelect();
    },
    typePicker: function () {
        this.setData({
            typePickerShow: true
        })
    },
    sureCallBack_type(e) {
        var that=this;
        this.setData({
            typePickerShow: false,
            typeFlag:true,
            typeVal: e.detail.choosedData[0].name,
            typeId: e.detail.choosedData[0].id,
            orderList:[],
            pageCurrent:1
        })
        that.orderSelect();
    },
    cancleCallBack_type() {
        this.setData({
            typePickerShow: false,
        })
    },
    datePicker: function () {
        this.setData({
            datePickerShow: true
        })
    },
    sureCallBack_date(e) {
        var that=this;
        that.setData({
            datePickerShow: false,
            dateFlag:true,
            dateVal: e.detail.choosedData[0].name,
            dateSign: e.detail.choosedData[0].sign,
            orderList:[],
            pageCurrent:1
        });
        that.orderSelect();
    },
    cancleCallBack_date() {
        this.setData({
            datePickerShow: false,
        })
    },
    orderSelect:function(){
        var that=this;
        if(that.data.dateSign==''){
            that.getOrder(that.data.orderStatus,that.data.typeId,'','');
        }else if(that.data.dateSign=='agoYear'){
            that.getOrder(that.data.orderStatus,that.data.typeId,'2018-01-01',util.orderCreatDate('agoYear'));
        }else{
            that.getOrder(that.data.orderStatus,that.data.typeId,util.orderCreatDate(that.data.dateSign),util.nowDate());
        }
    },
    onReachBottom: function () {
        let that = this;
        if (that.data.pageCurrent < that.data.pageNum) {
            that.setData({
                loading:true,
                loadingComplete:false,
                pageCurrent: that.data.pageCurrent + 1
            })
            that.getOrder(that.data.orderStatus,that.data.typeId,'','');
        }else if(that.data.pageCurrent >= that.data.pageNum){
            that.setData({
                loading:false,
                loadingComplete:true
            })
        }
    }
})
